package cn.dhnep.demo.controller;


import cn.dhnep.demo.pojo.Notice;
import cn.dhnep.demo.pojo.Order;
import cn.dhnep.demo.service.HomepageService;
import cn.dhnep.demo.service.OrderService;
import cn.dhnep.demo.util.Page;
import cn.dhnep.demo.util.ResultMap;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dhn
 * @since 2021-08-18
 */
@RestController
@RequestMapping("/order")
public class OrderController {


    @Resource
    private OrderService orderService;

    private Map<String,Object> result = new HashMap<>();


    @ApiOperation("订单分页查询")
    @RequestMapping("orderlist")
    public ResultMap<List<Order>> NoticeList(String orderNumber,String userName,String horsemanName,String orderState,Page page, @RequestParam("limit") Integer limit) throws UnsupportedEncodingException {
        if(orderNumber!=null){
            page.setOrderNumber(orderNumber);
        }
        if(userName!=null){
            page.setUserName(userName);
        }
        if(horsemanName!=null){
            page.setHorsemanName(horsemanName);
        }
        if(orderState!=null){
            page.setOrderState(orderState);
        }
        page.setRows(limit);
        List<Order>orderList=orderService.selectPageList(page);
        int totals=orderService.selectPageCount(page);
        page.setTotalRecord(totals);

        return new ResultMap<List<Order>>("",orderList,0,totals);
    }

    @ApiOperation("id查询订单")
    @RequestMapping("idqueryOrder")
    public void IdqueryOrder(Integer id, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Order order =orderService.idqueryOrder(id);
        System.out.println(order);
        request.setAttribute("order",order);
        session.setAttribute("order",order);
        request.getRequestDispatcher( "../page/order-edit.html" ).forward(request,response);
    }

    @GetMapping("idquerysession")
    public Map<String,Object> idquerysession(HttpServletRequest request){
        HttpSession session = request.getSession();
        Order order =(Order)session.getAttribute("order");
        result.put("order",order);
        return result;
    }

    @ApiOperation("修改订单")
    @RequestMapping("updateorder")
    public  Map<String,Object> Updateorder(@RequestBody Order order, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        int count =orderService.updateorder(order);
        if (count==1){
            result.put("result","success");
        }else{
            result.put("result","error");
        }
        return result;
    }

}

